import scrapy
import json
from huya.items import HuyaItem
class GoSpider(scrapy.Spider):
    name = 'go'
    # allowed_domains = ['huya.com']
    # start_urls = ['http://huya.com/']
    def start_requests(self):
        url = 'https://www.huya.com/g'
        yield scrapy.Request(url=url,callback=self.list_url)
    def list_url(self,response):
        url = response.xpath('//*[@id="js-game-list"]/li/a/@href').getall()
        bq = response.xpath('//*[@id="js-game-list"]/li/a/@data-gid').getall()
        for i,x in zip(bq,url):
            yield scrapy.Request(url=x,callback=self.ju_url,meta={'bq':i})
    def ju_url(self,response):
        url = 'https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId='+response.meta['bq']+'&tagAll=0&page=1'
        yield scrapy.Request(url=url,meta={'bq':response.meta['bq']},callback=self.jie)
    def jie(self,response):
        ye = json.loads(response.text)
        for i in range(1,ye['data']['totalPage']):
            url = 'https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=' + response.meta[
                'bq'] + '&tagAll=0&page='+str(i)
            yield scrapy.Request(url=url, meta={'bq': response.meta['bq']}, callback=self.qing)
    def qing(self,response):
        jd = json.loads(response.text)
        for i in jd['data']['datas']:
            url = 'https://www.huya.com/'+str(i.get('profileRoom'))
            yield scrapy.Request(url=url,meta={
                'gameFullName':i['gameFullName'],
                'nick':i['nick'],
                'totalCount':i['totalCount'],
                'bluRayMBitRate':i['bluRayMBitRate']
            },callback=self.parse)
    def parse(self, response):
        rr = response.xpath('//*[@id="activityCount"]/text()').getall()
        print('############################')
        print(response.meta['gameFullName'])
        txt = HuyaItem()
        txt['Type_name'] = response.meta['gameFullName']
        txt['sudu'] = response.meta['bluRayMBitRate']
        txt['name'] = response.meta['nick']
        txt['qi']= response.meta['totalCount']
        txt['dy'] = rr
        yield txt

